package com.lyxmaster.lib.view;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

/**
 * Created by LU-MAC on 2018/3/25.
 */

public class FirstComeActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        init();
    }

    private void init(){
        getPermission();
    }

    /**
     * 6.0以上系统获取权限
     */
    private void getPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String[] pers = new String[]{
                    Manifest.permission.ACCESS_FINE_LOCATION,   //位置信息
                    Manifest.permission.ACCESS_COARSE_LOCATION, //位置信息
                    Manifest.permission.CAMERA, //相机
                    Manifest.permission.READ_EXTERNAL_STORAGE,  //存储
                    Manifest.permission.VIBRATE,    //震动
                    Manifest.permission.WAKE_LOCK   //休眠
            };
            int permissionCheck = 0;
            for(String temp : pers){
                permissionCheck += this.checkSelfPermission(temp);
            }
            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                this.requestPermissions( // 请求授权
                        pers,0);// 自定义常量,任意整型
            } else {
                Log.d("li", "onRequestPermissionsResult: OK");
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 0:
                if (hasAllPermissionGranted(grantResults)) {
                    Log.d("li", "onRequestPermissionsResult: OK");
                } else {
                    finish();
                }
                break;
        }
    }

    private boolean hasAllPermissionGranted(int[] grantResults) {
        for (int grantResult : grantResults) {
            if (grantResult == PackageManager.PERMISSION_DENIED) {
                return false;
            }
        }
        return true;
    }
}
